/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.impl.module;

import java.lang.annotation.Annotation;

import net.paoding.rose.web.ControllerInterceptor;
import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Intercepted;

/**
 * {@link ControllerInterceptor}的一个简单封装，给被封装的拦截器一个命名。
 * <p>
 * 开发者在控制器配置 {@link Intercepted} 的allow和deny的名字指的就是这个名字
 * <p>
 * 
 * @author qieqie.wang@paoding.net
 * 
 */
public class NamedControllerInterceptor implements ControllerInterceptor {

	private ControllerInterceptor inner;
	private Object controller;
	private String name;
	private boolean applicationContextInterceptor;

	public NamedControllerInterceptor(String name, ControllerInterceptor inner) {
		this.setName(name);
		this.inner = inner;
	}

	public NamedControllerInterceptor(String name, ControllerInterceptor inner,
			boolean applicationContextInterceptor) {
		this.setName(name);
		this.inner = inner;
		this.applicationContextInterceptor = applicationContextInterceptor;
	}

	public void setController(Object controller) {
		this.controller = controller;
	}

	public Object getController() {
		return controller;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public ControllerInterceptor getInner() {
		return inner;
	}

	public boolean isApplicationContextInterceptor() {
		return applicationContextInterceptor;
	}

	@Override
	public int getPriority() {
		return inner.getPriority();
	}

	@Override
	public Class<? extends Annotation>[] getAnnotationClasses() {
		return inner.getAnnotationClasses();
	}
	
	@Override
	public boolean beEffectedByAnnotatedAtSuperClass(
			Class<? extends Annotation> annotationClazz) {
		return inner.beEffectedByAnnotatedAtSuperClass(annotationClazz);
	}

	@Override
	public Object before(Invocation invocation) throws Exception {
		if (this.controller == null || invocation.getController() == this.controller) {
			return inner.before(invocation);
		}
		return true;
	}

	@Override
	public Object after(Invocation invocation, Object instruction) throws Exception {
		if (this.controller == null || invocation.getController() == this.controller) {
			return inner.after(invocation, instruction);
		}
		return instruction;
	}

	@Override
	public void afterCompletion(Invocation invocation, Throwable ex) throws Exception {
		if (this.controller == null || invocation.getController() == this.controller) {
			inner.afterCompletion(invocation, ex);
		}
	}
	
	@Override
	public String toString() {
		return getName();
	}

}
